using LiJ.Common.Core;
using LiJ.Common.Core.Enums;
using LiJ.Common.Encrypt;
using LiJ.Common.Extention;
using LiJ.Framework.Msg.Mos;
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace LiJ.Framework.Msg
{
	internal static class WxMsgHelper
	{
		/// <summary>
		/// 验证签名方法
		/// </summary>
		/// <param name="token"></param>
		/// <param name="signature"></param>
		/// <param name="timestamp"></param>
		/// <param name="nonce"></param>
		/// <returns></returns>
		internal static ResultMo CheckSignature(string token, string signature, string timestamp, string nonce)
		{
			if (!(signature == WxMsgHelper.GenerateSignature(token, timestamp, nonce, "")))
			{
				return new ResultMo(ResultTypes.UnAuthorize, "签名验证失败！");
			}
			return new ResultMo();
		}

		/// <summary>
		/// 验证签名方法
		/// </summary>
		/// <param name="token"></param>
		/// <param name="timestamp"></param>
		/// <param name="nonce"></param>
		/// <param name="strEncryptMsg"></param>
		/// <returns></returns>
		internal static string GenerateSignature(string token, string timestamp, string nonce, string strEncryptMsg = "")
		{
			List<string> list = new List<string>
			{
				token,
				timestamp,
				nonce,
				strEncryptMsg
			};
			list.Sort();
			return Sha1.Encrypt(string.Join(string.Empty, list), Encoding.ASCII);
		}

		/// <summary>
		///  加密消息体
		/// </summary>
		/// <param name="sReplyMsg"></param>
		/// <param name="config"></param>
		/// <returns></returns>
		internal static ResultMo<string> EncryptMsg(string sReplyMsg, WxMsgConfig config)
		{
			string text;
			try
			{
				text = Cryptography.AesEncrypt(sReplyMsg, config.EncodingAesKey, config.AppId);
			}
			catch (Exception)
			{
				return new ResultMo<string>(ResultTypes.InnerError, "加密响应消息体出错！");
			}
			DateTime now = DateTime.Now;
			string text2 = now.ToUtcSeconds().ToString();
			string text3 = now.ToString("yyyyMMddHHssff");
			string value = WxMsgHelper.GenerateSignature(config.Token, text2, text3, text);
			if (string.IsNullOrEmpty(value))
			{
				return new ResultMo<string>(ResultTypes.InnerError, "生成签名信息出错！");
			}
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append("<xml>").Append("<Encrypt><![CDATA[").Append(text)
				.Append("]]></Encrypt>");
			stringBuilder.Append("<MsgSignature><![CDATA[").Append(value).Append("]]></MsgSignature>");
			stringBuilder.Append("<TimeStamp><![CDATA[").Append(text2).Append("]]></TimeStamp>");
			stringBuilder.Append("<Nonce><![CDATA[").Append(text3).Append("]]></Nonce>");
			stringBuilder.Append("</xml>");
			return new ResultMo<string>(stringBuilder.ToString());
		}

		/// <summary>
		/// 把xml文本转化成字典对象
		/// </summary>
		/// <param name="xml"></param>
		/// <param name="xmlDoc">返回格式化后的xml对象</param>
		/// <returns></returns>
		internal static Dictionary<string, string> ChangXmlToDir(string xml, out XmlDocument xmlDoc)
		{
			if (string.IsNullOrEmpty(xml))
			{
				xmlDoc = null;
				return null;
			}
			xmlDoc = new XmlDocument();
			xmlDoc.LoadXml(xml);
			XmlNodeList childNodes = xmlDoc.FirstChild.ChildNodes;
			Dictionary<string, string> dictionary = new Dictionary<string, string>(childNodes.Count);
			foreach (XmlNode item in childNodes)
			{
				dictionary[item.Name] = item.InnerText;
			}
			return dictionary;
		}
	}
}
